package jvlite.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
	private IAdvice advice;
	private Object target;

	public Object getProxy() {
		Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

				advice.beforeMethod(method);
				Object retVal = method.invoke(target, args);
				advice.afterMethod(method);

				return retVal;
			}
		});
		return proxy;
	}
	
	public IAdvice getAdvice() {
		return advice;
	}

	public void setAdvice(IAdvice advice) {
		this.advice = advice;
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

}